package com.xwh.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xwh.article.client.SystemService;
import com.xwh.article.entity.SoftwareVersionDownload;
import com.xwh.article.mapper.SoftwareDownloadMapper;
import com.xwh.article.service.SoftwareDownloadService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xwh
 **/
@Service
@RequiredArgsConstructor
@Transactional
public class SoftwareDownloadServiceImpl extends ServiceImpl<SoftwareDownloadMapper, SoftwareVersionDownload> implements SoftwareDownloadService {

    final SystemService systemService;

    @Override
    public List<SoftwareVersionDownload> listBySoftwareVersion(String softwareVersionId) {
        QueryWrapper<SoftwareVersionDownload> query = new QueryWrapper<>();
        query.eq("software_version_id", softwareVersionId);
        return list(query);
    }

    @Override
    public void add(List<SoftwareVersionDownload> list, String versionId) {
        // 先删除该版本所有的下载地址
        QueryWrapper<SoftwareVersionDownload> query = new QueryWrapper<>();
        query.eq("software_version_id", versionId);
        remove(query);
        // 再新增
        list.forEach(item -> {
            item.setSoftwareVersionId(versionId);
        });
        saveBatch(list);
    }

}
